package com.itbaizhan.itbaizhanlock.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZookeeperConfig {

    /**
     * Curator客户端
     * @return
     */
    @Bean
    public CuratorFramework getZKClient(){
        //1创建curator客户端
        CuratorFramework client =CuratorFrameworkFactory.builder()
                //zk连接地址  localhost: 连接的是在windows的zookeeper,2181是端口号
                .connectString("127.0.0.1:2181")
                //网话超时时间
                .sessionTimeoutMs(5000)
                //链接创建超时时间
                .connectionTimeoutMs(5000)
                //重试策略  1s重试3次
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .build();

        client.start();
        return client;
    }
}
